package com.slz3.business.module.systemManagement.sysMenu.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.systemManagement.sysMenu.domain.SysMenu;
import com.slz3.business.module.systemManagement.sysMenu.domain.SysMenuCondition;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class SysMenuResSafeValid {

    static boolean add(ServerRequest req, SysMenu sysMenu) {
        // T-O-D-O 唯一约束
        UUID parentId = sysMenu.getParentId();
        if (parentId != null){
            Optional<SysMenu> single = Services.sysMenu.single(parentId);
            if (!single.isPresent()){
                RR.resDetail(req).put(RR.SAFE_VALID, "上级菜单不存在");
            }
        }
        SysMenu params = new SysMenu();
        params.setSerName(sysMenu.getSerName());
        if(!CollectionUtils.isEmpty(Services.sysMenu.findAll(params))){
            RR.resDetail(req).put(RR.SAFE_VALID, "已经存在相同名字的菜单");
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean delete(ServerRequest req, SysMenuCondition sysMenuCondition) {
        List<SysMenu> sysMenuList = Services.sysMenu.findAllByUuids(sysMenuCondition.getUuidList());
        if (sysMenuList.stream().filter(
                item -> AuditFlag.isApproved(item.getSerAudit()) || AuditFlag.isReject(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能删除【已审核】或【被驳回】或【被删除】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysMenuList);
        }
        if(sysMenuList.stream()
                .anyMatch(item ->
                        !CollectionUtils.isEmpty(Repositorys.sysMenu.queryByParentId(item.getUuid())))){
            RR.resDetail(req).put(RR.SAFE_VALID, "不能删除含有子菜单的记录");
        }
        if (sysMenuList.stream().anyMatch(item ->
                !CollectionUtils.isEmpty(Repositorys.sysPermissions.queryByMenuUuid(item.getUuid())))) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能删除含有权限的记录");
        }
        return RR.hasResErr(req);
    }

    static boolean modify(ServerRequest req, SysMenu sysMenu) {
        Optional<SysMenu> sysMenuDB = Services.sysMenu.single(sysMenu.getUuid());
        if (sysMenuDB.isPresent()) {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysMenuDB.get());
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        }
        UUID parentId = sysMenu.getParentId();
        if (parentId != null){
            Optional<SysMenu> single = Services.sysMenu.single(parentId);
            if (!single.isPresent()){
                RR.resDetail(req).put(RR.SAFE_VALID, "上级菜单不存在");
            }
        }
        SysMenu params = new SysMenu();
        params.setSerName(sysMenu.getSerName());
        List<SysMenu> sysMenus = Services.sysMenu.findAll(params);
        if(!CollectionUtils.isEmpty(sysMenus) &&
                sysMenus.stream().anyMatch(item -> !item.getUuid().equals(sysMenu.getUuid()))){
            RR.resDetail(req).put(RR.SAFE_VALID, "已经存在相同名字的菜单");
        }
        return RR.hasResErr(req);
    }

    static boolean info(ServerRequest req) {
        Optional<SysMenu> sysMenuDB = Services.sysMenu.single(UUID.fromString(req.queryParam("uuid").get()));
        if (VE.isEmpty(sysMenuDB)) {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysMenuDB.get());
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditSubmit(ServerRequest req, SysMenuCondition sysMenuCondition) {
        List<SysMenu> sysMenuList = Services.sysMenu.findAllByUuids(sysMenuCondition.getUuidList());
        if (sysMenuList.stream().filter(
                item -> !AuditFlag.isWaitSubmit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能审核【被删除】状态或不是【待提交】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysMenuList);
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean audit(ServerRequest req, SysMenuCondition sysMenuCondition) {
        List<SysMenu> sysMenuList = Services.sysMenu.findAllByUuids(sysMenuCondition.getUuidList());
        if (sysMenuList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能审核【被删除】状态或不是【待审核】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysMenuList);
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditReject(ServerRequest req, SysMenuCondition sysMenuCondition) {
        List<SysMenu> sysMenuList = Services.sysMenu.findAllByUuids(sysMenuCondition.getUuidList());
        if (sysMenuList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit())||!AuditFlag.isApproved(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能驳回【被删除】状态或不是【待审核】或【已审核】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysMenuList);
        }
        return RR.hasResErr(req);
    }

    
    static final boolean usingEnable(ServerRequest req, SysMenuCondition sysMenuCondition) {
        List<SysMenu> sysMenuList = Services.sysMenu.findAllByUuids(sysMenuCondition.getUuidList());
        if (sysMenuList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能启用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysMenuList);
        }
        return RR.hasResErr(req);
    }


    static final boolean usingDisable(ServerRequest req, SysMenuCondition sysMenuCondition) {
        List<SysMenu> sysMenuList = Services.sysMenu.findAllByUuids(sysMenuCondition.getUuidList());
        if (sysMenuList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能禁用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysMenuList);
        }
        return RR.hasResErr(req);
    }
}
